package com.tuanzi.loan.business.repository.statistics.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductShowUv.LOAN_PRODUCT_SHOW_UV;
import static com.tuanzi.loan.generated.jooq.stat.tables.StatUserActivityData.STAT_USER_ACTIVITY_DATA;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang.math.NumberUtils;
import org.jooq.Condition;
import org.jooq.GroupField;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectField;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.repository.statistics.ActivityAverageInterceptStatRepository;
import com.tuanzi.loan.business.repository.statistics.ActivityParticipateStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinInterceptStatRequest;
import com.tuanzi.loan.vo.response.miniApps.articleLibrary.HealthHeadlineResponse;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinInterceptStatResponse;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;
import com.tuanzi.server.loan_base.util.DateUtils;


/**
 * 人均参与截流统计
 * @author weibifeng
 */
@Repository
public class ActivityAverageInterceptStatRepositoryImpl extends JOOQBaseRepositoryPlus implements ActivityAverageInterceptStatRepository {
    private static final String ALL_FIELD = "'全部'";
    private static final String ALL_FIELD_ID = "'-1000'";
    private DecimalFormat decimalFormat1 = new DecimalFormat("#.##%");
	private DecimalFormat decimalFormat2 = new DecimalFormat("0.00");
    
    @Autowired
    private ActivityParticipateStatRepository activityParticipateStatRepository;
    
    public Map<String,String> getProdMap() {
    	return activityParticipateStatRepository.getProdMap();
    }
    
    public List<CommProductPrdid> getProduct(){
        return activityParticipateStatRepository.getProduct();
    }

    public Map<String,String> getChannelMap() {
        return activityParticipateStatRepository.getChannelMap();
    }

    public List<CommProductChannel> getChannel(){
        return activityParticipateStatRepository.getChannel();
    }

    public List<CommPlatform> getPlatform(){
        return activityParticipateStatRepository.getPlatform();
    }

    public List<CommUserType> getUserType(){
        return activityParticipateStatRepository.getUserType();
    }
    
    public Map<String,String> getSpaceMap() {
    	return activityParticipateStatRepository.getSpaceMap();
    }

    public List<CommAdvertise> getSpace(){
        return activityParticipateStatRepository.getAD();
    }

    public Map<String,String> getActivityMap() {
        return activityParticipateStatRepository.getActivityMap();
    }

    public List<LoanProductInfo> getActivity(){
        return activityParticipateStatRepository.getActivity();
    }

    private <K,V> Map<V,K> revert(Map<K,V> map){
        Map<V,K> revertMap = new HashMap<>();
        for (K key: map.keySet()){
            revertMap.put(map.get(key), key);
        }
        return revertMap;
    }
    
    /**
     * 获取登陆uv
     * @param searchRequest
     * @return
     */
    private Map<String ,Integer> getLoginUvMap(ActivityAverageJoinInterceptStatRequest searchRequest) {
    	StringBuffer fromSql = new StringBuffer(" FROM (SELECT d.date ,d.channel ,d.prdid ,d.login_uv FROM stat.stat_user_activity_data d WHERE 1 = 1 ");
    	// 时间区间
    	if (Objects.nonNull(searchRequest.getStartDate())) {
    		fromSql.append(" and d.date >= '" + DateUtils.formatDateToYyyyMMdd(searchRequest.getStartDate()) + "'");
    	} else {
    		fromSql.append(" and d.date >= '" + DateUtils.formatDateToYyyyMMdd(new Date()) + "'"); // 默认当前时间
    	}
    	if (Objects.nonNull(searchRequest.getEndDate())) {
    		fromSql.append(" and d.date <= '" + DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + "'");
    	} else {
    		fromSql.append(" and d.date <= '" + DateUtils.formatDateToYyyyMMdd(new Date()) + "'"); // 默认当前时间
    	}
    	fromSql.append(" GROUP BY d.date, d.channel, d.prdid ) t ");
    	
    	StringBuffer selectSql = new StringBuffer(" SELECT t.date ,SUM(t.login_uv) login_uv ");
    	StringBuffer whereSql = new StringBuffer(" WHERE 1 = 1 ");
    	StringBuffer groupSql = new StringBuffer(" GROUP BY t.date ");
    	
    	// 产品
    	if (Objects.isNull(searchRequest.getProductIdList()) || searchRequest.getProductIdList().isEmpty()) {
        	// 全部展开
    		selectSql.append(" ,t.prdid ");
    		groupSql.append(" ,t.prdid ");
        } else if(isSelectQueryAll(searchRequest.getProductIdList())) {
        	// 查询全部
        	selectSql.append(",'-1000' as prdid ");
        } else {
        	selectSql.append(" ,t.prdid ");
        	whereSql.append(" and t.prdid in ( " + intlistToSqlPart(searchRequest.getProductIdList()) + ") ");
        	groupSql.append(" ,t.prdid ");
        }
    	
    	// 渠道
    	if (Objects.isNull(searchRequest.getChannelIdList()) || searchRequest.getChannelIdList().isEmpty()) {
        	// 全部展开
    		selectSql.append(" ,t.channel ");
    		groupSql.append(" ,t.channel ");
        } else if(isSelectQueryAll(searchRequest.getChannelIdList())) {
        	// 查询全部
        	selectSql.append(",'-1000' as channel ");
        } else {
        	selectSql.append(" ,t.channel ");
        	whereSql.append(" and t.channel in ( " + strlistToSqlPart(searchRequest.getChannelIdList()) + ") ");
        	groupSql.append(" ,t.channel ");
        }
    	
    	 String sql = selectSql.append(fromSql).append(whereSql).append(groupSql).toString();
         
         Map<String, Integer> map = new HashMap<String, Integer>();
         dslContext.fetch(sql).map(new RecordMapper<Record, ActivityAverageJoinInterceptStatResponse>() {
 			@Override
 			public ActivityAverageJoinInterceptStatResponse map(Record record) {
 				String date = record.get("date", String.class);
 				String prod = record.get("prdid", String.class);
 				String channel = record.get("channel", String.class);
 				String loginUv = record.get("login_uv", String.class);
 				map.put(date+"_"+prod+"_"+channel, NumberUtils.stringToInt(loginUv));
 				return null;
 			}
 		});
    	
    	return map;
    }
    
    private Map<String ,Integer> getActivityUvMap(ActivityAverageJoinInterceptStatRequest searchRequest) {
    	StringBuffer dateSql = new StringBuffer("");
    	// 时间区间
    	if (Objects.nonNull(searchRequest.getStartDate())) {
    		dateSql.append(" and s.date >= '" + DateUtils.formatDateToYyyyMMdd(searchRequest.getStartDate()) + "'");
    	} else {
    		dateSql.append(" and s.date >= '" + DateUtils.formatDateToYyyyMMdd(new Date()) + "'"); // 默认当前时间
    	}
    	if (Objects.nonNull(searchRequest.getEndDate())) {
    		dateSql.append(" and s.date <= '" + DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + "'");
    	} else {
    		dateSql.append(" and s.date <= '" + DateUtils.formatDateToYyyyMMdd(new Date()) + "'"); // 默认当前时间
    	}
    	
    	StringBuffer acSelectSql = new StringBuffer(" SELECT ac.date ,SUM(ac.activity_uv) activity_uv ");
    	StringBuffer acFrom = new StringBuffer(" FROM (SELECT s.date ,s.channel ,s.platform ,s.prdid ,s.user_type ,s.activity_uv FROM stat.stat_user_activity_data s WHERE 1 = 1 ");
    	acFrom.append(dateSql);
    	acFrom.append(" GROUP BY s.date, s.channel ,s.platform ,s.prdid ,s.user_type ) ac ");
    	StringBuffer acWhereSql = new StringBuffer(" WHERE 1 = 1 ");
    	StringBuffer acGroupSql = new StringBuffer(" GROUP BY ac.date ");
    	
    	// 产品
    	if (Objects.isNull(searchRequest.getProductIdList()) || searchRequest.getProductIdList().isEmpty()) {
        	// 全部展开
    		acSelectSql.append(" ,ac.prdid ");
    		acGroupSql.append(" ,ac.prdid ");
        } else if(isSelectQueryAll(searchRequest.getProductIdList())) {
        	// 查询全部
        	acSelectSql.append(",'-1000' as prdid ");
        } else {
        	acSelectSql.append(" ,ac.prdid ");
        	acWhereSql.append(" and ac.prdid in ( " + intlistToSqlPart(searchRequest.getProductIdList()) + ") ");
        	acGroupSql.append(" ,ac.prdid ");
        }
    	
    	// 渠道
        if (Objects.isNull(searchRequest.getChannelIdList()) || searchRequest.getChannelIdList().isEmpty()) {
        	// 全部展开
    		acSelectSql.append(" ,ac.channel ");
    		acGroupSql.append(" ,ac.channel ");
        } else if(isSelectQueryAll(searchRequest.getChannelIdList())) { 
        	// 查询全部
        	acSelectSql.append(",'-1000' as channel ");
        } else {
        	acSelectSql.append(" ,ac.channel ");
        	acWhereSql.append(" and ac.channel in ( " + strlistToSqlPart(searchRequest.getChannelIdList()) + ") ");
        	acGroupSql.append(" ,ac.channel ");
        }
    	
        // 平台
        if (Objects.isNull(searchRequest.getPlatformList()) || searchRequest.getPlatformList().isEmpty()) {
        	// 全部展开
    		acSelectSql.append(" ,ac.platform ");
    		acGroupSql.append(" ,ac.platform ");
        } else if(isSelectQueryAll(searchRequest.getPlatformList())) {
        	// 查询全部
        	acSelectSql.append(",'-1000' as platform ");
        } else {
        	acSelectSql.append(" ,ac.platform ");
        	acWhereSql.append(" and ac.platform in ( " + strlistToSqlPart(searchRequest.getPlatformList()) + ") ");
        	acGroupSql.append(" ,ac.platform ");
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserTypeList()) || searchRequest.getUserTypeList().isEmpty()) {
        	// 全部展开
    		acSelectSql.append(" ,ac.user_type ");
    		acGroupSql.append(" ,ac.user_type ");
        } else if(isSelectQueryAll(searchRequest.getUserTypeList())) {
        	// 查询全部
        	acSelectSql.append(",'-1000' as user_type ");
        } else {
        	acSelectSql.append(" ,ac.user_type ");
        	acWhereSql.append(" and ac.user_type in ( " + intlistToSqlPart(searchRequest.getUserTypeList()) + ") ");
        	acGroupSql.append(" ,ac.user_type ");
        }
        
        String sql = acSelectSql.append(acFrom).append(acWhereSql).append(acGroupSql).toString();
        
        Map<String, Integer> map = new HashMap<String, Integer>();
        dslContext.fetch(sql).map(new RecordMapper<Record, ActivityAverageJoinInterceptStatResponse>() {
			@Override
			public ActivityAverageJoinInterceptStatResponse map(Record record) {
				String date = record.get("date", String.class);
				String prod = record.get("prdid", String.class);
				String channel = record.get("channel", String.class);
				String platform = record.get("platform", String.class);
				String userType = record.get("user_type", String.class);
				String activityUv = record.get("activity_uv", String.class);
				map.put(date+"_"+prod+"_"+channel+"_"+platform+"_"+userType, NumberUtils.stringToInt(activityUv));
				return null;
			}
		});
    	
    	return map;
    }
    
    private String intlistToSqlPart(List<Integer> list) {
		if(list == null || list.size() == 0) {
			return "";
		}
		StringBuffer sbf = new StringBuffer();
		for(int i = 0; i<list.size(); i++) {
			Integer s = Integer.valueOf(list.get(i)+"");
			if(sbf.length() == 0) {
				sbf.append("'"+s+"'");
			} else {
				sbf.append(",'"+s+"'");
			}
		}
		return sbf.toString();
	}
    
    private String strlistToSqlPart(List<String> list) {
		if(list == null || list.size() == 0) {
			return "";
		}
		StringBuffer sbf = new StringBuffer();
		for(String s : list) {
			if(sbf.length() == 0) {
				sbf.append("'"+s+"'");
			} else {
				sbf.append(",'"+s+"'");
			}
		}
		return sbf.toString();
	}
    
    private SelectQuery getListQuery(ActivityAverageJoinInterceptStatRequest searchRequest){
    	Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();
        List<GroupField> groupFields = new ArrayList<>();
        
        selectFields.add(STAT_USER_ACTIVITY_DATA.DATE);
        groupFields.add(STAT_USER_ACTIVITY_DATA.DATE);
        if (Objects.nonNull(searchRequest.getStartDate())) {
            condition = condition.and(STAT_USER_ACTIVITY_DATA.DATE.ge(new java.sql.Date(searchRequest.getStartDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndDate())) {
        	condition = condition.and(STAT_USER_ACTIVITY_DATA.DATE.le(new java.sql.Date(searchRequest.getEndDate().getTime())));
        }
        
        // 产品
        if (Objects.isNull(searchRequest.getProductIdList()) || searchRequest.getProductIdList().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.PRDID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.PRDID);
        } else if(isSelectQueryAll(searchRequest.getProductIdList())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("prdid"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.PRDID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.PRDID);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.PRDID.in(searchRequest.getProductIdList()));
        }
       
        // 渠道
        if (Objects.isNull(searchRequest.getChannelIdList()) || searchRequest.getChannelIdList().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.CHANNEL);
            groupFields.add(STAT_USER_ACTIVITY_DATA.CHANNEL);
        } else if(isSelectQueryAll(searchRequest.getChannelIdList())) { 
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("channel"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.CHANNEL);
            groupFields.add(STAT_USER_ACTIVITY_DATA.CHANNEL);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.CHANNEL.in(searchRequest.getChannelIdList()));
        }
        
        // 平台
        if (Objects.isNull(searchRequest.getPlatformList()) || searchRequest.getPlatformList().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.PLATFORM);
            groupFields.add(STAT_USER_ACTIVITY_DATA.PLATFORM);
        } else if(isSelectQueryAll(searchRequest.getPlatformList())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("platform"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.PLATFORM);
            groupFields.add(STAT_USER_ACTIVITY_DATA.PLATFORM);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.PLATFORM.in(searchRequest.getPlatformList()));
        }
        
        // 用户类型
        if (Objects.isNull(searchRequest.getUserTypeList()) || searchRequest.getUserTypeList().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.USER_TYPE);
            groupFields.add(STAT_USER_ACTIVITY_DATA.USER_TYPE);
        } else if(isSelectQueryAll(searchRequest.getUserTypeList())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("USER_TYPE"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.USER_TYPE);
            groupFields.add(STAT_USER_ACTIVITY_DATA.USER_TYPE);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.USER_TYPE.in(searchRequest.getUserTypeList()));
        }
        
        // 活动
        if (Objects.isNull(searchRequest.getActivityList()) || searchRequest.getActivityList().isEmpty()) {
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.ACTIVITY_ID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.ACTIVITY_ID);
        } else if(isSelectQueryAll(searchRequest.getActivityList())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("activity_id"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.ACTIVITY_ID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.ACTIVITY_ID);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.ACTIVITY_ID.in(searchRequest.getActivityList()));
        }
        
        // 广告
        if (Objects.isNull(searchRequest.getAdIdList()) || searchRequest.getAdIdList().isEmpty()){
        	// 全部展开
        	selectFields.add(STAT_USER_ACTIVITY_DATA.SPACE_ID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.SPACE_ID);
        } else if(isSelectQueryAll(searchRequest.getAdIdList())) {
        	// 查询全部
        	selectFields.add(DSL.field(ALL_FIELD_ID).as("space_id"));
        } else {
            selectFields.add(STAT_USER_ACTIVITY_DATA.SPACE_ID);
            groupFields.add(STAT_USER_ACTIVITY_DATA.SPACE_ID);
            condition = condition.and(STAT_USER_ACTIVITY_DATA.SPACE_ID.in(searchRequest.getAdIdList()));
        }
        
        return dslContext.select(selectFields)
        		.select(DSL.sum(STAT_USER_ACTIVITY_DATA.SHOW_UV).as("show_uv"))
        		.select(DSL.sum(STAT_USER_ACTIVITY_DATA.CLICK_UV).as("click_uv"))
                .from(STAT_USER_ACTIVITY_DATA)
                .where(condition)
                .groupBy(groupFields)
                .orderBy(STAT_USER_ACTIVITY_DATA.DATE.desc())
                .getQuery();
    }
    
    public Page<ActivityAverageJoinInterceptStatResponse> getlistPage(ActivityAverageJoinInterceptStatRequest searchRequest,
			Pageable pageable) {
    	SelectQuery selectQuery = getListQuery(searchRequest); 
    	Map<String ,Integer> acMap = getActivityUvMap(searchRequest);
    	Map<String ,Integer> loginMap = getLoginUvMap(searchRequest);
    	Page<ActivityAverageJoinInterceptStatResponse> page = search(selectQuery, pageable, record ->{
			ActivityAverageJoinInterceptStatResponse response = new ActivityAverageJoinInterceptStatResponse();
			response.setDate(record.get(0,Date.class));
			String ProdId = record.get(1,String.class);
			response.setProductId(ProdId);
			response.setProductName(getProdMap().get(ProdId));
			String channelId = record.get(2,String.class);
			response.setChannelId(channelId);
			response.setChannelName(getChannelMap().get(channelId));
			String platform = record.get(3,String.class);
			response.setPlatform(platform);
			String userTypeId = record.get(4,String.class);
			response.setUserTypeId(userTypeId);
			if("1".equals(userTypeId)) {
				response.setUserType("新用户");
			} else if("0".equals(userTypeId)) {
				response.setUserType("老用户");
			} else if("-1000".equals(userTypeId)) {
				response.setUserType("全部");
			} else {
				response.setUserType("未知");
			}
			
			Long activityId = record.get(5,Long.class);
			response.setActivityId(activityId);
			response.setActivityName(getActivityMap().get(String.valueOf(activityId)));
			String adId = record.get(6,String.class);
			String adName = null;
			if(getSpaceMap().containsKey(adId)) {
				adName = getSpaceMap().get(adId);
			} else {
				adName = adId;
			}
			response.setAdId(adId);
			response.setAdName(adName);
			response.setLoginUv(loginMap.get(DateUtils.formatDateToYyyyMMdd(response.getDate())+"_"+response.getProductId()+"_"+response.getChannelId()));
			String key = DateUtils.formatDateToYyyyMMdd(response.getDate())+"_"+response.getProductId()
					+"_"+response.getChannelId()+"_"+response.getPlatform()+"_"+response.getUserTypeId();
			Integer activityUv = acMap.get(key);
			Integer showUv = record.get(7,Integer.class);
			Integer clickUv = record.get(8,Integer.class);
			String perShowAct = null;
			String perClickAct = null;
			String perClickShow = null;
			
			if(activityUv != null && activityUv != 0) {
				if(showUv != null) {
					perShowAct = decimalFormat2.format(showUv.doubleValue() / activityUv.doubleValue());
				}
				if(clickUv != null) {
					perClickAct = decimalFormat2.format(clickUv.doubleValue() / activityUv.doubleValue());
				}
			}
			if(showUv!=null && showUv != 0 && clickUv != null) {
				perClickShow = decimalFormat1.format(clickUv / showUv.doubleValue());
			}
			
			response.setPlatform("-1000".equals(response.getPlatform()) ? "全部" : response.getPlatform());
			response.setActivityUv(activityUv);
			response.setShowUv(showUv);
			response.setClickUv(clickUv);
			response.setPerShowAct(perShowAct);
			response.setPerClickAct(perClickAct);
			response.setPerClickShow(perClickShow);
			return response;
		});
    	acMap.clear();
    	return page;
	}
    
    public List<ActivityAverageJoinInterceptStatResponse> getAllList(ActivityAverageJoinInterceptStatRequest searchRequest) {
    	SelectQuery selectQuery = getListQuery(searchRequest);
    	Map<String ,Integer> acMap = getActivityUvMap(searchRequest);
    	Map<String ,Integer> loginMap = getLoginUvMap(searchRequest);
    	List<ActivityAverageJoinInterceptStatResponse> resultList = selectQuery.fetch(record -> {
    		ActivityAverageJoinInterceptStatResponse response = new ActivityAverageJoinInterceptStatResponse();
			response.setDate(record.get(0,Date.class));
			String ProdId = record.get(1,String.class);
			response.setProductId(ProdId);
			response.setProductName(getProdMap().get(ProdId));
			String channelId = record.get(2,String.class);
			response.setChannelId(channelId);
			response.setChannelName(getChannelMap().get(channelId));
			String platform = record.get(3,String.class);
			response.setPlatform(platform);
			String userTypeId = record.get(4,String.class);
			response.setUserTypeId(userTypeId);
			if("1".equals(userTypeId)) {
				response.setUserType("新用户");
			} else if("0".equals(userTypeId)) {
				response.setUserType("老用户");
			} else if("-1000".equals(userTypeId)) {
				response.setUserType("全部");
			} else {
				response.setUserType("未知");
			}
			
			Long activityId = record.get(5,Long.class);
			response.setActivityId(activityId);
			response.setActivityName(getActivityMap().get(String.valueOf(activityId)));
			String adId = record.get(6,String.class);
			String adName = null;
			if(getSpaceMap().containsKey(adId)) {
				adName = getSpaceMap().get(adId);
			} else {
				adName = adId;
			}
			response.setAdId(adId);
			response.setAdName(adName);
			response.setLoginUv(loginMap.get(DateUtils.formatDateToYyyyMMdd(response.getDate())+"_"+response.getProductId()+"_"+response.getChannelId()));
			String key = DateUtils.formatDateToYyyyMMdd(response.getDate())+"_"+response.getProductId()
					+"_"+response.getChannelId()+"_"+response.getPlatform()+"_"+response.getUserTypeId();
			Integer activityUv = acMap.get(key);
			Integer showUv = record.get(7,Integer.class);
			Integer clickUv = record.get(8,Integer.class);
			String perShowAct = null;
			String perClickAct = null;
			String perClickShow = null;
			
			if(activityUv != null && activityUv != 0) {
				if(showUv != null) {
					perShowAct = decimalFormat2.format(showUv.doubleValue() / activityUv.doubleValue());
				}
				if(clickUv != null) {
					perClickAct = decimalFormat2.format(clickUv.doubleValue() / activityUv.doubleValue());
				}
			}
			if(showUv!=null && showUv != 0 && clickUv != null) {
				perClickShow = decimalFormat1.format(clickUv / showUv.doubleValue());
			}
			
			response.setPlatform("-1000".equals(response.getPlatform()) ? "全部" : response.getPlatform());
			response.setActivityUv(activityUv);
			response.setShowUv(showUv);
			response.setClickUv(clickUv);
			response.setPerShowAct(perShowAct);
			response.setPerClickAct(perClickAct);
			response.setPerClickShow(perClickShow);
			return response;
    	});
    	acMap.clear();
    	return resultList;
    }
    
    
    
    /******检查是否查询全部*********/
    public boolean isSelectQueryAll(List selectList) {
    	if(selectList == null || selectList.size() == 0) {
    		return false;
    	}
    	for(int i=0; i<selectList.size(); i++) {
    		String s = String.valueOf(selectList.get(i));
    		if("-1000".equals(s)) {
    			return true;
    		}
    	}
    	return false;
    }
    
    public void cleanCache() {
    	activityParticipateStatRepository.cleanCache();
    }
    
    @Override
    protected TableImpl getTable() {
        return LOAN_PRODUCT_SHOW_UV;
    }

}
